package com.macro.mall.tiny.controller;

import com.macro.mall.tiny.common.api.CommonPage;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.dto.ProductAttributeParam;
import com.macro.mall.tiny.mbg.model.PmsProductAttribute;
import com.macro.mall.tiny.service.PmsProductAttributeService;
import com.macro.mall.tiny.vo.ProductAttrInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/productAttribute")
@Api(tags="pmsProductAttribute")
@Tag(name="pmsProductAttribute",description = "商品属性参数")
@PreAuthorize("@ex.hasAuthority('/productAttribute')")
public class PmsProductAttributeController {

    @Autowired
    private PmsProductAttributeService pmsProductAttributeService;

    //查询单个商品属性
    @GetMapping("/{id}")
    @ApiOperation("查询单个商品属性")
    public CommonResult<PmsProductAttribute> getById(@PathVariable Long id){
        log.info("查询单个商品属性：{}",id);

        PmsProductAttribute pmsProductAttribute=pmsProductAttributeService.getById(id);
        return CommonResult.success(pmsProductAttribute);
    }

    //新增商品属性信息
    @PostMapping("/create")
    @ApiOperation("新增商品属性信息")
    public CommonResult create(@RequestBody PmsProductAttribute pmsProductAttribute){
        log.info("新增商品属性信息:{}",pmsProductAttribute);

        Integer code=pmsProductAttributeService.create(pmsProductAttribute);
        return code==2? CommonResult.success():CommonResult.failed();
    }

    //根据分类查询属性列表或参数列表
    @GetMapping("/list/{cid}")
    @ApiOperation("根据分类查询属性列表或参数列表")
//    @ApiImplicitParams({@ApiImplicitParam(name="type",value="0表示属性，1表示参数",required = true,paramType = "query",dataType = "integer",defaultValue = "0")})
    public CommonResult<CommonPage<PmsProductAttribute>> getList(
            @PathVariable Long cid,
            @RequestParam(value="type",required = true) Integer type,
            @RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value="pageSize",defaultValue = "5") Integer pageSize){

        log.info("根据分类查询属性列表或参数列表:{},{},{},{}",cid,type,pageNum,pageSize);

        List<PmsProductAttribute> pmsProductAttributeList=pmsProductAttributeService.getList(cid,type,pageNum,pageSize);
        return CommonResult.success(CommonPage.restPage(pmsProductAttributeList));
    }

    //修改商品属性信息
    @PostMapping("/update/{id}")
    @ApiOperation("修改商品属性信息")
    public CommonResult updateById(@PathVariable Long id, @RequestBody ProductAttributeParam productAttributeParam){
        log.info("修改商品属性信息；{},{}",id,productAttributeParam);

        Integer code=pmsProductAttributeService.updateById(id,productAttributeParam);
        return code==1? CommonResult.success():CommonResult.failed();

    }


    //批量删除商品属性
    @PostMapping("/delete")
    @ApiOperation("批量删除商品属性")
    public CommonResult deleteByIdList(@RequestParam(value="ids") List<Long> ids){
        log.info("批量删除商品属性:{}",ids);

        Integer number=ids.size();
        Integer code=pmsProductAttributeService.deleteByIdList(ids);
        if(code==number+1){
            return CommonResult.success();
        }else{
            return CommonResult.failed("部分或者全部删除未成功");
        }
    }

    //根据商品分类的id获取商品属性及其属性分类
    @GetMapping("/attrInfo/{productCategoryId}")
    @ApiOperation("根据商品分类的id获取商品属性及其属性分类")
    public CommonResult<List<ProductAttrInfo>> getAttrInfo(@PathVariable Long productCategoryId){
        log.info("根据商品分类的id获取商品属性及其属性分类:{}",productCategoryId);

        List<ProductAttrInfo> productAttrInfoList=pmsProductAttributeService.getAttrInfo(productCategoryId);
        return CommonResult.success(productAttrInfoList);
    }


}
